package com.bio.action.basicinfo;

import java.util.Calendar;
import net.sf.json.JSONObject;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import com.bio.action.AbstractAction;
import com.bio.entity.basicinfo.Basicinfo;
import com.bio.service.basicinfo.BasicinfoService;
import com.bio.utils.Environment;

@Namespace("/basicinfo")
@Results( {
		@Result(name = "list", location = "/WEB-INF/views/basicinfo/listBasicinfos.jsp", type = "dispatcher"),
		@Result(name = "listByAdmin", location = "/WEB-INF/views/basicinfo/listBasicinfosByAdmin.jsp", type = "dispatcher"),
		@Result(name = "tolist", location = "basicinfo!list", type = "redirect"),
		@Result(name = "edit", location = "/WEB-INF/views/basicinfo/editBasicinfo.jsp", type = "dispatcher"),
		@Result(name = "add", location = "/WEB-INF/views/basicinfo/addBasicinfo.jsp", type = "dispatcher")
		})
public class BasicinfoAction extends AbstractAction<Basicinfo, BasicinfoService> {

	private static final long serialVersionUID = -285030935778408178L;

	private Basicinfo basicinfo;
	
	private Integer type;
	
	public BasicinfoAction(BasicinfoService service) {
		super(service);
	}

	public String list(){
		Long companyId = Environment.Session.getCompanyId();
		pagination.setCurrentIndex(this.getCurrentIndex());
		if(null != this.type && !this.type.equals("")){
			this.pager = this.service.findPagerByType(pagination, type, companyId);
		}else{
		    this.pager = this.service.findPagerByCompanyId(pagination, companyId);
		}
		return "list";
	}
	
	public String listByAdmin(){
		pagination.setCurrentIndex(this.getCurrentIndex());
		this.pager = this.service.findAllByPager(pagination);
		return "listByAdmin";
	}
	
	public String add(){
		return "add";
	}
	
	public String delete(){
		this.service.remove(id);
		return "tolist";
	}
	
	public String edit(){
		this.basicinfo = this.service.get(id);
		return "edit";
	}
	
	public String save(){
		Calendar now = Calendar.getInstance();
		this.basicinfo.setCreatedDate(now);
		
		this.basicinfo.setCompanyId(Environment.Session.getCompanyId());
		
		this.service.save(basicinfo);
		return "tolist";
	}
	
	public String update(){
		Basicinfo old = this.service.get(basicinfo.getId());
		this.basicinfo.setCreatedDate(old.getCreatedDate());
		this.basicinfo.setUpdatedDate(Calendar.getInstance());
		
		this.service.save(basicinfo);
		return "tolist";
	}
	
	public static void main(String[] args) {
		Basicinfo basicinfo = new Basicinfo();
		basicinfo.setName("实际");
		JSONObject j = new JSONObject();
		j.accumulate("basicInfo", basicinfo);
		System.out.println(j.toString());
		
	}

	public Basicinfo getBasicinfo() {
		return basicinfo;
	}

	public void setBasicinfo(Basicinfo basicinfo) {
		this.basicinfo = basicinfo;
	}

	public Integer getType() {
		return type;
	}

	public void setType(Integer type) {
		this.type = type;
	}
}
